define(function(require, exports, module) {
	'use strict';
    //require('./notiPanelCtrl');
    ////
    ////var getTimeToString = require('core/services/noticeService');
    //var rk = require('rk');
    
    var rk = require('rk');
    
    
    var uService = require('core/services/biz/noticeService');
    var appsTypeConstants = uService.appsTypeConstants;
    var getTimeToString = uService.getTimeToString;
    $.widget('rk.notiContentCtrl', $.rk.oaContentCtrl, {
        _language: "zh",
        _create: function () {
            var me = this;
            var elem = me.element;
            if (navigator.userLanguage) {
                me._language = navigator.userLanguage.substring(0, 2).toLowerCase();
            } else {
                me._language = navigator.language.substring(0, 2).toLowerCase();
            }
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if (opt.type == "announcement") {
                rk.post("/json/oa_announcement/detail.action", {aid: opt.id}, function (json) {
                    if (json.status === 0) {
                        var created = json.body.created;
                        var nowTime = json.nowTime;
                        json.body.created = me.getTimeToString(created, nowTime, me);
                        var createTime = new Date(created);
                        json.body.createTime = createTime.getFullYear() + "年" + (createTime.getMonth() + 1) + "月" + createTime.getDate() + "日";
                        if (json.body.stickDate) {
                            var stickDate = json.body.stickDate;
                            var stickDateTime = new Date(stickDate);
                            var month = (stickDateTime.getMonth() + 1) + "";
                            month = month.length == 1 ? "0" + month : month;
                            var day = stickDateTime.getDate() + "";
                            day = day.length == 1 ? "0" + day : day;
                            json.body.stickDate = stickDateTime.getFullYear() + "-" + month + "-" + day;
                        }
                        //var html = rk.template("tpl_noti_announcement_detail", json);
                        var temp = require('oa/tmpl/notice/template/tpl_noti_announcement_detail.tpl');
                        var html = rk.templateText(temp,json);
                        elem.html(html);
                    } else {
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                });
                me._initAnnouncementListner();
            }
        },
        _initAnnouncementListner: function () {
            var me = this;
            var elem = me.element;
            elem.on('click', "[act='set_top']", function () {
                elem.find("[act='set_top_div']").toggle();
                var daysHtml = elem.find("[act='ann_update_stickType_days']").html();
                if (daysHtml === "") {
                    var stick_days = {
                        3: rk.i18n('ANNOUNCEMENT_TIME_THREE_DAY'),
                        7: rk.i18n('ANNOUNCEMENT_TIME_ONE_WEEK'),
                        14: rk.i18n('ANNOUNCEMENT_TIME_TWO_WEEK'),
                        30: rk.i18n('ANNOUNCEMENT_TIME_ONE_MONTH'),
                        60: rk.i18n('ANNOUNCEMENT_TIME_TWO_MONTH'),
                        90: rk.i18n('ANNOUNCEMENT_TIME_THREE_MONTH'),
                        180:rk.i18n('ANNOUNCEMENT_TIME_SIX_MONTH'),
                        360: rk.i18n('ANNOUNCEMENT_TIME_ONE_YEAR'),
                        "-1": rk.i18n('ANNOUNCEMENT_TIME_FOREVER')
                    };
                    var stick_daysHtml = "";
                    var daysValue = elem.find("[act='ann_update_stickType_days_hidden']").val();
                    for (var key in stick_days) {
                        if (key === daysValue) {
                            stick_daysHtml += "<option value='" + key + "' selected>" + stick_days[key] + "</option>"
                        } else {
                            stick_daysHtml += "<option value='" + key + "'>" + stick_days[key] + "</option>"
                        }
                    }
                    elem.find("[act='ann_update_stickType_days']").html(stick_daysHtml);
                    if (daysValue) {
                        elem.find("[act='ann_update_stickType_days']").val(daysValue);
                    }
                    elem.find("[act='ann_update_stickType_days']").selectmenu();
                    elem.find(".ui-check-buttonset").buttonset();
                    elem.find(".ui-radio-buttonset").buttonset();
                }
                if (!elem.find("[act='ann_update_stickType_date']").val()) {
                    var nowDate = new Date();
                    var month = (nowDate.getMonth() + 1) + "";
                    month = month.length == 1 ? "0" + month : month
                    var day = nowDate.getDate() + "";
                    day = day.length == 1 ? "0" + day : day;
                    var defaultDate = nowDate.getFullYear() + "-" + month + "-" + day;
                    elem.find("[act='ann_update_stickType_date']").val(defaultDate);
                }
                elem.find("[act='ann_update_stickType_date']").datepicker();
            });
            elem.on('click', "[act='anno_delete']", function () {
                var del = $(this);
                rk.confirm(rk.i18n('NOTICE_CONFIRM_DELETE'),{okText:rk.i18n('DELETE')}, function () {
                    rk.post("/json/oa_announcement/delete.action", {aid: del.attr("aid")}, function (json) {
                        if (json.status === 0) {
                            //删除成功，刷新
                            var panel = rk.oa.getPanel();
                            if(panel) {
                                require.async(['./notiPanelCtrl.js'],function(){
                                    panel.notiPanelCtrl("loaddata", "announcement");
                                });
                            }
                            me.closeMe();
                        } else if (json.status === 211001) {
                            //没有权限
                            rk.noticeError(rk.i18n('NOTICE_NO_ACCESS'));
                        } else if (json.status === 100000) {
                            //系统错误
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                    });
                }, function () {
                    //取消
                });
            });
            elem.on('click', '[act="ann_update_stickType_days_radio"]', function () {
                elem.find("[act='ann_update_stickType_date']").attr("disabled", "disabled");
                elem.find("[act=ann_update_stickType_days]").attr("disabled", null);
            });
            elem.on('click', '[act="ann_update_stickType_date_radio"]', function () {
                elem.find("[act='ann_update_stickType_days']").attr("disabled", "disabled");
                elem.find("[act='ann_update_stickType_date']").attr("disabled", null);
            });

            elem.on('click', "[act='ann_update_submit']", function () {
                var form = elem.find("[act='ann_update_form']");
                var formData = form.serializeArray();
                var params = {};
                $.each(formData, function (i, json) {
                    if (json.name === "stickDate" && json.value) {
                        var value = json.value;
                        var valueArray = value.split("-");
                        var year = valueArray[0];
                        var month = valueArray[1];
                        var day = valueArray[2];
                        json.value = new Date(year - 0, month - 1, day - 0).getTime();
                    }
                    params["announcement." + json.name] = json.value;
                });
                rk.post("/json/oa_announcement/update-announcement.action", params, function (json) {
                    if (json.status === 0) {
                        //置顶成功
                        var panel = rk.oa.getPanel();
                        if(panel) {
                            require.async(['./notiPanelCtrl.js'],function(){
                                panel.notiPanelCtrl("loaddata", "announcement");
                            });
                        }
                        elem.find("[act='set_top_div']").hide();
                    } else if (json.status === 211001) {
                        //没有权限
                        rk.noticeError(rk.i18n('NOTICE_NO_ACCESS'));
                    } else if (json.status === 100000) {
                        //系统错误
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                });
            });
        },
        getTimeToString: getTimeToString
    });
});